package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.redis.RedisService.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.redis.RedisService.IUserInfoRedisService;
import cn.wolfcode.luowowo.redis.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/strategies")
public class StrategiesController {
    //注入攻略服务接口
    @Autowired
    private IStrategyService strategyService;
    //注入攻略主题服务接口
    @Autowired
    private IStrategyThemeService strategyThemeService;

    //注入客户缓存服务接口
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    //注入攻略评价服务接口
    @Autowired
    private IStrategyCommentService strategyCommentService;
    //注入攻略VO缓存服务接口
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;


    @GetMapping("/detail")
    public Object detail(String id) {

        strategyStatisVORedisService.viewnumIncrease(id, 1);
        return JsonResult.success(strategyService.get(id));
    }

    @GetMapping("/query")
    public Object query(StrategyQuery qo) {
        return JsonResult.success(strategyService.query(qo));
    }

    @GetMapping("/themes")
    public Object themes() {
        return JsonResult.success(strategyThemeService.list());
    }

    //攻略评论分页
    @GetMapping("/comments")
    public Object comments(StrategyQuery qo) {
        Page<StrategyComment> page = strategyService.queryComments(qo);
        return JsonResult.success(page);
    }

    //需要登录才能发布
    @RequireLogin
    //攻略评论发布
    @PostMapping("/addComment")
    public Object addComment(StrategyComment strategyComment, HttpServletRequest request, String strategyId) {
        //先通过token获取用户的信息
        UserInfo user = userInfoRedisService.getUserByToken(request.getHeader("token"));
        //将用户的信息拷贝到攻略评论里，只有相同字段的才会拷贝
        //底层是通过内省查找字段，然后将相同字段的值拷贝到目标对象，并赋值
        BeanUtils.copyProperties(user, strategyComment);
        strategyComment.setUserId(user.getId());
        //然后保存
        strategyCommentService.save(strategyComment);
        //攻略数评论加一
        strategyStatisVORedisService.addCommentVo(strategyId);
        return JsonResult.success();
    }

    //点赞
    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid, String sid, HttpServletRequest request) {
        String token = request.getHeader("token");
        UserInfo user = userInfoRedisService.getUserByToken(token);
        strategyCommentService.commentThumb(cid, user.getId());
        return JsonResult.success();
    }

    @RequireLogin
    @GetMapping("/info")
    public Object info(UserInfo userInfo) {
        return JsonResult.success();
    }

    //统计数据统计
    @GetMapping("/statisVo")
    public Object statisVo(String sid) {

        StrategyStatisVO vo = strategyStatisVORedisService.getStatisVo(sid);
        return JsonResult.success(vo);
    }

    //收藏数据添加
    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid, @UserParam UserInfo userInfo) {
        boolean favor = strategyStatisVORedisService.favor(sid, userInfo.getId());
        return JsonResult.success(favor);
    }

    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid, @UserParam UserInfo userInfo) {

        boolean strategyThumbup = strategyStatisVORedisService.strategyThumbup(sid, userInfo.getId());
        return JsonResult.success(strategyThumbup);
    }
}
